package com.oa.pm.service.impl;

import com.joneying.common.annotation.CommonService;
import com.joneying.common.web.response.Resp;
import com.oa.core.PageResult;
import com.oa.core.enumm.ProcessDefineEnum;
import com.oa.core.exception.BusinessException;
import com.oa.core.exception.DataIsEmptyException;
import com.oa.core.utils.DateUtil;
import com.oa.core.utils.ExcelUtil;
import com.oa.pm.dto.SkillUpgradTrainingDTO;
import com.oa.pm.dto.SkillUpgradTrainingParamDTO;
import com.oa.pm.entity.InductionTrainingPersonnel;
import com.oa.pm.entity.SkillUpgradTraining;
import com.oa.pm.mapper.SkillUpgradTrainingMapper;
import com.oa.pm.service.IInductionTrainingPersonnelService;
import com.oa.pm.service.ISkillUpgradTrainingService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.oa.pm.vo.SkillUpgradTrainingDetailsVO;
import com.oa.pm.vo.SkillUpgradTrainingListVO;
import com.oa.system.dto.ApplyApprovalAddDTO;
import com.oa.system.service.IApplyApprovalService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 技能提升培训表 服务实现类
 * </p>
 *
 * @author syb123
 * @since 2019-01-22
 */
@Service
public class SkillUpgradTrainingServiceImpl extends ServiceImpl<SkillUpgradTrainingMapper, SkillUpgradTraining> implements ISkillUpgradTrainingService {

    private final SkillUpgradTrainingMapper skillUpgradTrainingMapper;

    @Autowired
    private IInductionTrainingPersonnelService inductionTrainingPersonnelService;

    @Autowired
    private IApplyApprovalService applyApprovalService;

    @Autowired
    public SkillUpgradTrainingServiceImpl(SkillUpgradTrainingMapper skillUpgradTrainingMapper) {
        this.skillUpgradTrainingMapper = skillUpgradTrainingMapper;
    }

    @Override
    @CommonService
    public PageResult<SkillUpgradTraining> findList(Integer pageNum, Integer pageSize) {
        if(pageNum == null){
            List<SkillUpgradTraining> list = skillUpgradTrainingMapper.findList();
            return new PageResult<>(list);
        }else{
            // 当前页，总条数 构造 page 对象
            Page<SkillUpgradTraining> page = new Page<>(pageNum, pageSize);
            //查询
            List<SkillUpgradTraining> list = skillUpgradTrainingMapper.findList(page);
            return new PageResult<>(list);
        }
    }

    /**
    * 根据id查询
    */
    @Override
    @CommonService
    public SkillUpgradTrainingDetailsVO findSkillUpgradTrainingById(String id){
        return skillUpgradTrainingMapper.findSkillUpgradTrainingDetails(id);
    }

    @Override
    @CommonService
    public PageResult<SkillUpgradTrainingListVO> findSkillUpgradTrainingList(SkillUpgradTrainingParamDTO skillUpgradTrainingParamDTO) {
        // 当前页，总条数 构造 page 对象
        Page<SkillUpgradTraining> page = new Page<>(skillUpgradTrainingParamDTO.getPageNum(), skillUpgradTrainingParamDTO.getPageSize());
        return new PageResult<>(skillUpgradTrainingMapper.findSkillUpgradTrainingList(page, skillUpgradTrainingParamDTO));
    }

    @Transactional(rollbackFor = Exception.class)
    @CommonService
    @Override
    public Resp addSkillUpgradTraining(SkillUpgradTrainingDTO skillUpgradTrainingDTO) throws BusinessException, DataIsEmptyException {
        SkillUpgradTraining skillUpgradTraining = new SkillUpgradTraining();
        BeanUtils.copyProperties(skillUpgradTrainingDTO, skillUpgradTraining);

        if(insert(skillUpgradTraining)){
            List<InductionTrainingPersonnel> inductionTrainingPersonnels = skillUpgradTrainingDTO.getInductionTrainingPersonnels();
            inductionTrainingPersonnels.forEach(d -> {
                d.setTrainingId(skillUpgradTraining.getId());
                d.setType(3);
            });
            if(inductionTrainingPersonnelService.insertBatch(inductionTrainingPersonnels)){
                ApplyApprovalAddDTO applyApprovalAddDTO = new ApplyApprovalAddDTO();
                applyApprovalAddDTO.setApplyId(skillUpgradTraining.getId());
                applyApprovalAddDTO.setApprovalProcessId(ProcessDefineEnum.TRAINING.getId());
                applyApprovalAddDTO.setApplyApprovalName(skillUpgradTrainingDTO.getDepartment() + ProcessDefineEnum.TRAINING.getText());
                applyApprovalAddDTO.setApprovalUserId(skillUpgradTrainingDTO.getUserId());
                return applyApprovalService.addApproval(applyApprovalAddDTO);
            }
        }
        throw new BusinessException("提交申请失败!");
    }

    @Override
    public void export(SkillUpgradTrainingParamDTO skillUpgradTrainingParamDTO, HttpServletResponse response) {
        List<SkillUpgradTrainingListVO> skillUpgradTrainingListVOS;
        if(skillUpgradTrainingParamDTO.getPageNum() != null){
            Page<SkillUpgradTraining> page = new Page<>(skillUpgradTrainingParamDTO.getPageNum(), skillUpgradTrainingParamDTO.getPageSize());
            skillUpgradTrainingListVOS = skillUpgradTrainingMapper.findSkillUpgradTrainingList(page, skillUpgradTrainingParamDTO);
        }else{
            skillUpgradTrainingListVOS = skillUpgradTrainingMapper.findSkillUpgradTrainingList(new Page(), skillUpgradTrainingParamDTO);
        }
        String[] rows = {"id", "申请ID", "用户名称", "部门名称", "类型名称", "申请时间", "审核状态", "备注"};
        try {
            ExcelUtil.downloadExcel("培训申请列表" + DateUtil.getDayMonth(), rows, skillUpgradTrainingListVOS, "培训申请列表" + DateUtil.getDayMonth()+".xls", response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
